import cv2 as cv
from PyCmpltrtok.common import sep


def on_change(event):
    pass


def set_2_trackbars_for_value(title, mag=5, factor=100, base=10, mag_init=0, factor_init=1):
    cv.namedWindow(title)
    cv.imshow(title, np.zeros((50, 1000), dtype=np.uint8))
    MAG = 'magnitude'
    FACTOR = 'factor'
    NSIZE = 'neighbor size'
    DIL_KSIZE = 'dilate kernel'
    DIL_ITER = 'dilate iteration'
    cv.createTrackbar(MAG, title, mag_init, mag, on_change)
    cv.createTrackbar(FACTOR, title, factor_init, factor, on_change)
    cv.createTrackbar(NSIZE, title, 3, 10, on_change)
    cv.createTrackbar(DIL_KSIZE, title, 1, 10, on_change)
    cv.createTrackbar(DIL_ITER, title, 1, 10, on_change)

    def getValue():
        mag_v = cv.getTrackbarPos(MAG, title)
        factor_v = cv.getTrackbarPos(FACTOR, title)
        neighbor_size = cv.getTrackbarPos(NSIZE, title)
        v = base ** mag_v * factor_v
        dilate_ksize = cv.getTrackbarPos(DIL_KSIZE, title)
        dilate_iter = cv.getTrackbarPos(DIL_ITER, title)
        return v, neighbor_size, dilate_ksize, dilate_iter

    return getValue

if '__main__' == __name__:
    import sys
    import numpy as np
    from as_book_ex import getHarrisCorners

    def _main():
        sep('Load img')
        WINDOW_TITLE = 'Harris'
        path = r'D:\_const\large_data\pic\harris_corner\chuch.jpg'
        argc = len(sys.argv)
        if argc >= 2:
            path = sys.argv[1]
        img = cv.imread(path, cv.IMREAD_GRAYSCALE)
        imgc = cv.imread(path, cv.IMREAD_COLOR)
        if img is None or imgc is None:
            print(f'{path} cannot be read as image!', file=sys.stderr)
            sys.exit(1)

        sep('Harris')
        def applyThresh(thresh, neighbor_size, dilate_ksize, dilate_iter):
            sep(f'{thresh}, neighbor size={neighbor_size} dilate ksize={dilate_ksize} iter={dilate_iter}')
            harrisCorners = getHarrisCorners(img, thresh, blockSize=neighbor_size)
            print('Showing ...')
            cv.imshow(WINDOW_TITLE, harrisCorners)

            kernel = np.ones([dilate_ksize, dilate_ksize], np.uint8)
            harrisCorners = cv.dilate(harrisCorners, kernel, iterations=dilate_iter)
            mask = cv.bitwise_not(harrisCorners)
            green = np.stack([np.zeros_like(harrisCorners), harrisCorners, np.zeros_like(harrisCorners)], axis=2)
            rest = cv.bitwise_and(imgc, imgc, mask=mask)
            show = green + rest
            cv.imshow(WINDOW_TITLE + '_show', show)

        get_value_fn = set_2_trackbars_for_value('bar', base=0.1, mag_init=2)

        while True:
            print('> Close the image windows to continue, press q on the windows to quit ...')
            thresh, neighbor_size, dilate_ksize, dilate_iter = get_value_fn()
            applyThresh(thresh, neighbor_size, dilate_ksize, dilate_iter)
            key = cv.waitKey(0)
            if key == ord('Q') or key == ord('q'):
                break
            cv.destroyWindow(WINDOW_TITLE)
            cv.destroyWindow(WINDOW_TITLE + '_show')

    _main()
    sep('All Over')